﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Threading;

namespace Win7RovMobileApp
{
    public partial class ConfirmationScreen : PhoneApplicationPage
    {
        // color animation on OrderButton
        private DispatcherTimer ColorAnimationTimer;         
        private int ColorAnimationCounter;


        public ConfirmationScreen()
        {
            InitializeComponent();
            DataContext = App.UserPreferences;

            ColorAnimationTimer = new DispatcherTimer();
            ColorAnimationTimer.Interval = new TimeSpan(0, 0, 0, 0, 150);
            ColorAnimationTimer.Tick += new EventHandler(RunColorAnimation);   
            ColorAnimationTimer.Start();
        }


        private void RunColorAnimation(object obj, EventArgs e)
        {
            int chosenColor = ColorAnimationCounter % 3;
            switch(chosenColor)
            {
                case 0:
                    {
                        OrderButton.Foreground = new SolidColorBrush(Colors.Blue);        
                        break;
                    }
                case 1:
                    {
                        OrderButton.Foreground = GetColorFromHexa("#FF004EFF");
                        break;
                    }
                case 2:
                    {
                        OrderButton.Foreground = GetColorFromHexa("#FF0076FF");
                        break;
                    }
            }

            ColorAnimationCounter++;            
        }


        private SolidColorBrush GetColorFromHexa(string hexaColor)
        {
            return new SolidColorBrush(
                Color.FromArgb(
                    Convert.ToByte(hexaColor.Substring(1, 2), 16),
                    Convert.ToByte(hexaColor.Substring(3, 2), 16),
                    Convert.ToByte(hexaColor.Substring(5, 2), 16),
                    Convert.ToByte(hexaColor.Substring(7, 2), 16)
                )
            );
        }


        private void PlaceOrder_Click(object sender, RoutedEventArgs e)
        {
            //  TODO
            /*
             *  Hai sa prevenim cumpararea unei rovignete, daca exista deja o rovigneta achizitio. ptr numarul de inmatriculare cerut... 
             *  si acea rovigneta veche e activa inca.
             *  => ii spunem sa astepte, sau ii dam OK si cerem confirmare, sth like that
            */

            MessageBox.Show("Comanda a fost trimisa cu succes!\nVa multumim!");
            
            //reset all fields for next user preferences
            App.UserPreferences = new SavedOptions();
            
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

    }
}   